package com.zn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zn.Vo.CardAndCareerVo;
import com.zn.Vo.CareerAndAdminVo;
import com.zn.bean.Admin;
import com.zn.bean.Card;
import com.zn.bean.Career;
import com.zn.mapper.CareerMapper;
import com.zn.service.AdminService;
import com.zn.service.CardService;
import com.zn.service.CareerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zn.utils.Result;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 码农小周
 * @since 2023-05-12
 */
@Service
public class CareerServiceImpl extends ServiceImpl<CareerMapper, Career> implements CareerService {

    @Resource
    private AdminService adminService;

    @Resource
    private CardService cardService;

    @Override
    public Result<Page<CareerAndAdminVo>> CareerList(Long page, Long pageSize, String careerName) {
        Page<Career> careerPage = new Page<>(page, pageSize);
        Page<CareerAndAdminVo> careerAndAdminVoPage = new Page<>();


        LambdaQueryWrapper<Career> careerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        careerLambdaQueryWrapper.like(StringUtils.isNotEmpty(careerName), Career::getCareerName, careerName)
                .orderByDesc(Career::getUpdateTime);
        this.page(careerPage, careerLambdaQueryWrapper);

        BeanUtils.copyProperties(careerPage, careerAndAdminVoPage, "records");


        List<CareerAndAdminVo> CareerAndAdminVoList = careerPage.getRecords().stream().map(item -> {

            CareerAndAdminVo careerAndAdminVo = new CareerAndAdminVo();
            BeanUtils.copyProperties(item, careerAndAdminVo);

            Admin dbadmin = adminService.getById(item.getAdminId());

            careerAndAdminVo.setUpdateName(dbadmin.getAdminName());

            return careerAndAdminVo;

        }).collect(Collectors.toList());

        careerAndAdminVoPage.setRecords(CareerAndAdminVoList);

        return Result.success(careerAndAdminVoPage);

    }

    @Override
    public Result<String> addCareer(Career career) {

        if (StringUtils.isEmpty(career.getCareerName())) {
            return Result.error(501, "请输入参数");
        }

        LambdaQueryWrapper<Career> careerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        careerLambdaQueryWrapper.eq(Career::getCareerName, career.getCareerName());

        long count = this.count(careerLambdaQueryWrapper);

        if (count > 0) {
            return Result.error(501, "该职业已存在");
        }

        boolean save = this.save(career);


        return save ? Result.success("新增成功") : Result.error(501, "新增失败");
    }

    @Override
    public Result<String> editCareer(Career career) {
        if (StringUtils.isEmpty(career.getCareerName())) {
            return Result.error(501, "请输入参数");
        }

        //   careerId必须存在
        if (null == career.getCareerId()) {
            throw new RuntimeException();
        }

        Career dbCareer = this.getById(career.getCareerId());


//        数据库设置职业名为唯一索引，当数据库数据和修改数据一致时，将修改数据设置为空
        if (dbCareer.getCareerName().equals(career.getCareerName())) {
            career.setCareerName(null);
        }


        LambdaQueryWrapper<Career> careerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        careerLambdaQueryWrapper.eq(Career::getCareerName, career.getCareerName());

        long count = this.count(careerLambdaQueryWrapper);

        if (count > 0) {
            return Result.error(501, "该职业已存在");
        }

        boolean b = this.updateById(career);


        return b ? Result.success("修改成功") : Result.error(501, "修改失败");
    }

    @Override
    public Result<String> delCareer(Long id) {

        if (null == id) {
            throw new RuntimeException();
        }

        LambdaQueryWrapper<Card> careerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        careerLambdaQueryWrapper.eq(Card::getCareerId, id);

        long count = cardService.count(careerLambdaQueryWrapper);

        if (count > 0) {
            return Result.error(501, "该职业已经被使用，无法删除");
        }

        boolean b = this.removeById(id);


        return b ? Result.success("删除成功") : Result.error(501, "删除失败");

    }
}
